package com.bjpowernode.services;
import com.bjpowernode.beans.Activity;
import com.bjpowernode.beans.Page;
import com.bjpowernode.mapper.ActivityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

@Service
public class ActivityServicesImp implements ActivityServices{
    @Autowired
    ActivityMapper activityMapper;

    @Override
    public ArrayList<Activity> getAll() {
        return activityMapper.getAll();
    }

    @Override
    public Page getAll(Page page) {

//        // 当前页（查询条件）
//        private Integer currentPage = 1;
//        // 每页显示的记录条数（查询条件）
//        private Integer rowsPerPage = 5;
//        // 每页最多显示的记录条数(配置)
//        private Integer maxRowsPerPage =100;
//        // 总页数
//        private Integer totalPages;
//        // 总记录数（需要计算）
//        private Integer totalRows;
//        // 显示几个卡片(配置)
//        private Integer visiblePageLinks = 5;
//        //需要显示的数据集合
//        private List data;

        //利用数据库的select count(*) from xxx
        int count = activityMapper.getCount(page.getSearchMap());
        //当前页
        int currentPage = page.getCurrentPage();
        //总行数
        int totalRows=count;
        //每页行数
        int rowsPerPage = page.getRowsPerPage();
        //总页数
        int totalPages=0;
        if(totalRows%rowsPerPage==0){
            totalPages=totalRows/rowsPerPage;
        }
        else{
            totalPages=totalRows/rowsPerPage+1;
        }

        //查询准备填充的数据（带limit的）
        ArrayList<Activity> all = activityMapper.getAll((currentPage-1)*rowsPerPage, rowsPerPage,page.getSearchMap());
        //填充page数据
        //填充查询的数据集合
        page.setData(all);
        //填充总行数
        page.setTotalRows(totalRows);
        //填充总页数
        page.setTotalPages(totalPages);
        return page;
    }


    @Override
    public Activity get(String id) {
        return  activityMapper.get(id);
    }

    @Override
    public int add(Activity activity) {


        return  activityMapper.add(activity);
    }

    @Override
    public int edit(Activity activity) {

        return activityMapper.edit(activity);
    }

    @Override
    public int del(String[] ids) {
        return activityMapper.del(ids);
    }

    @Override
    public Activity getIdAndRemark(String id) {
        return activityMapper.getIdAndRemark(id);
    }
}
